<?php


class Template {
	
	/**
	 * Указатель на родительский класс
	 *
	 * @var MF_Main
	 */
	private $_top;
	
	private $_parent;
	
	const _default_xml = "template.xml";
	
	
	public $xml_path;
	public $defaul_template;
	public $stnd_content;
	public $preview_dir;
	
	
	/**
	 * Ссылка на объект типа File с данными настроек шаблонов
	 *
	 * @var File
	 */
	private $_xml_path;
	
	private $_stnd_content = array();
	private $_defaul_template;
	private $_preview_dir;
	private $_template_list = array();
	private $_template_attr = array();
	private $_template_content = array();
	
	public function __construct($top,$parent)
	{
		$this->_top =& $top;
		$this->_parent =& $parent;
		
		unset($this->xml_path);
		unset($this->defaul_template);
		unset($this->stnd_content);
		unset($this->preview_dir);
		
		try {
			$this->xml_path = self::_default_xml;
		} catch (Exception $e) { 
			try {
				$this->xml_path = $this->_top->_config->general->config_path.$this->_top->_config->templates->template_ini;
			} catch (Exception $e) { }
			
		}
		
		$this->_preview_dir = $this->_top->_config->templates->template_preview_path; 
	}
	
	private function parseXML() {
		$xmlDoc = $this->_top->Utils->ParseXML($this->xml_path);
		// ЗАполняем массив с информацией о предустоновленом контенте
		$stndContentNodes = $xmlDoc->getElementsByTagName("stnd_content");
		if ($stndContentNodes!=null) {
			for ($s=0;$s<$stndContentNodes->getLength();$s++) {
				$this->_stnd_content[$stndContentNodes->item($s)->getAttribute('id')] =
					array(
						'type' => $stndContentNodes->item($s)->getAttribute('content_type'),
						'id' => $stndContentNodes->item($s)->getAttribute('content_id')
					);
			}
		}
		
		/*$previewDirNode = $xmlDoc->getElementsByTagName("preview_dir");
		if ($previewDirNode!=null) {
			$this->_preview_dir = $previewDirNode->item(0)->getAttribute('path');
		}*/
		
		// Заполняем массив с информацией о шаблонах
		$defaultTemplate = $xmlDoc->getElementsByTagName("default_template");
		if ($defaultTemplate!=null) {
			$this->_defaul_template = $defaultTemplate->item(0)->getAttribute('key_name');
		}
		
		
		$templateNodes = $xmlDoc->getElementsByTagName("template");
		if ($templateNodes!=null) {
			$totalTemplate = $templateNodes->getLength();
			for ($t=0;$t<$totalTemplate;$t++) {
				$template_key_name = $templateNodes->item($t)->getAttribute("key_name");
				if ($templateNodes->item($t)->getAttribute("choosable")=='1') {
					array_push($this->_template_list,$template_key_name);
				}
				
				# Загрузка атрибутов шаблона
				$this->_template_attr[$template_key_name] = array(); 
				$template_attr_list = $templateNodes->item($t)->attributes;
				for ($a=0;$a<$template_attr_list->getLength();$a++) {
					$this->_template_attr[$template_key_name][$template_attr_list->item($a)->getName()] = $template_attr_list->item($a)->getValue();
				}
				
				# Загружка информации о контенте
				$this->_template_content[$template_key_name] = array();
				$contentNodes = $templateNodes->item($t)->getElementsByTagName('content');
				if ($contentNodes!=null) {
					for ($c=0;$c<$contentNodes->getLength();$c++) {
						$contentArrt = array();
						$content_attr_list = $contentNodes->item($c)->attributes;
						for ($a=0;$a<$content_attr_list->getLength();$a++) {
							$contentArrt[$content_attr_list->item($a)->getName()] = $content_attr_list->item($a)->getValue();
						}
						
						$contentArrt['xml'] = $contentNodes->item($c)->hasChildNodes() ?
							$contentNodes->item($c)->childNodes : null;
						
						array_push($this->_template_content[$template_key_name],$contentArrt);
					}
				}
			}
		}
	}
	
	public function __set($name,$value) {
		if ($name=="xml_path") {
			$this->_xml_path = $this->_top->Utils->File($value);
			$this->parseXML(); 
		}
	}
	
	public function __get($name) {
		if ($name=="xml_path") {
			return $this->_xml_path->full_path;
		} elseif ($name=="defaul_template") {
			return $this->_defaul_template;
		} elseif ($name=="stnd_content") {
			return $this->_stnd_content;
		} elseif ($name=="preview_dir") {
			return $this->_preview_dir;
		}
		else {
			return false;
		}
		
	}
	

	public function add() {
		throw $this->_top->Utils->PushError("Template::getById method NOT IN USE","Системная ошибка");
	}
	
	public function getStndContent($id) {
		return $this->_stnd_content[$id];
	}
	
	
	/**
	 * Возвращает массив объектов шаблонов
	 *
	 * @return array
	 */
	public function getList() {
		return $this->_template_list;
	}
	
	public function getAttr($template_name,$attr_name) {
		return $this->_template_attr[$template_name][$attr_name];
	}
	
	public function getContentList($template_name) {
		return $this->_template_content[$template_name];
	}
	
}
?>